#include "Engine.h"

TSS::Font *font = NULL;

int keyPressCode = 0;
int keyReleaseCode = 0;
int mouseButton = 0;
int moveX = 0;
int moveY = 0;
int posX = 0;
int posY = 0;
int wheel = 0;
double delta = 0;


void Game_Render3d() { }
void Game_End() { }


bool Game_Preload()
{
	g_engine->setAppTitle("TSS Engine");
	g_engine->setScreen(1024, 768, 32, false);
	return true;
}


bool Game_Init(HWND hWnd)
{
	font = new TSS::Font("Arial Bold", 30);
	if(!font)
	{
		debug << "Error creating font" << std::endl;
		return false;
	}
	return true;
}

void Game_Update(double deltaTime)
{
	delta = deltaTime;
}

void Game_Render2d()
{
	std::ostringstream os;
	os.imbue(std::locale("english-us"));
	os << "DELTA: " << TSS::ToString(delta, 4) << std::endl;
	os << "Core FPS: " << g_engine->getCoreFrameRate() << std::endl;
	os << "SCREEN FPS: " << g_engine->getScreenFrameRate() << std::endl;
	os << "KEY PRESS: " << keyPressCode << std::endl;
	os << "KEY RELEASE: " << keyReleaseCode << std::endl;
	os << "MOUSE MOVE: " << posX << "," << posY << std::endl;
	os << "MOUSE CLICK: " << mouseButton << std::endl;
	os << "MOUSE MOTION: " << moveX << "," << moveY << std::endl;
	os << "MOUSE WHEEL: " << wheel << std::endl;
	font->Print(100, 20, os.str());

	mouseButton = 0;
	wheel = 0;
}

void Game_Event(TSS::IEvent* e)
{
	switch(e->getID())
	{
	case TSS::EVENT_KEYPRESS:
		{
			TSS::KeyPressEvent* kpe = (TSS::KeyPressEvent*) e;
			keyPressCode = kpe->keycode;
			if(keyPressCode == DIK_ESCAPE)
			{
				g_engine->Shutdown();
			}
			break;
		}
	case TSS::EVENT_KEYRELEASE:
		{
			TSS::KeyReleaseEvent* kre = (TSS::KeyReleaseEvent*) e;
			keyReleaseCode = kre->keycode;
			break;
		}
	case TSS::EVENT_MOUSEMOVE:
		{
			TSS::MouseMoveEvent* mme = (TSS::MouseMoveEvent*) e;
			posX = mme->posx;
			posY = mme->posy;
			break;
		}
	case TSS::EVENT_MOUSECLICK:
		{
			TSS::MouseClickEvent* mce = (TSS::MouseClickEvent*) e;
			mouseButton = mce->button + 1;
			break;
		}
	case TSS::EVENT_MOUSEMOTION:
		{
			TSS::MouseMotionEvent* mme = (TSS::MouseMotionEvent*) e;
			moveX = mme->deltax;
			moveY = mme->deltay;
			break;
		}
	case TSS::EVENT_MOUSEWHEEL:
		{
			TSS::MouseWheelEvent* mwe = (TSS::MouseWheelEvent*) e;
			wheel = mwe->wheel;
			break;
		}
	default:
		{
			break;
		}
	}
}